package com.qilu.pe.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.qilu.pe.R;
import com.qilu.pe.base.Config;
import com.qilu.pe.support.util.Logg;
import com.qilu.pe.ui.activity.PaySuccessActivity;
import com.ruitu.arad.util.ToastUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "develop_debug_log";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Config.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        Logg.d("onPayFinish, errCode = " + resp.errCode);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage("微信支付结果" + resp.errCode);
//            builder.show();

            if (resp.errCode == 0) {
                Intent intent = new Intent(this, PaySuccessActivity.class);
                startActivity(intent);
            } else if (resp.errCode == -1) {
                ToastUtils.showShort("微信支付错误!");
            } else if (resp.errCode == -2) {
                ToastUtils.showShort("您取消了微信支付");
            }
            finish();
        }
    }
}
